<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    var a = [1, 3, 6, 3, 23, 76, 1, 34, 222, 6, 456, 221];

    function quickSort(array) {
      var quick = function (arr) {
        if (arr.length <= 1) return arr;
        const len = arr.length;
        // 将数组的长度右移一位向下取整
        const index = Math.floor(len >> 1);

        // 删除下标为index的元素,splice的返回值为被删除元素的数组
        const pivot = arr.splice(index, 1)[0];
        const left = [];
        const right = [];
        for (let i = 0; i < len; i++) {
          if (arr[i] > pivot) {
            right.push(arr[i]);
          } else if (arr[i] <= pivot) {
            left.push(arr[i]);
          }
        }
        return quick(left).concat([pivot], quick(right));
      };
      const result = quick(array);
      return result;
    }
    console.log(quickSort(a));
  </script>
</html>
